﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lswweb.Domain;
using Lswweb.Model;
using Lswweb.Common;

namespace Lswweb.Controls
{
    public class CartItemList : SkinnedGridView<IShoppingCartItem>
    {
        private IShoppingCart cart = ShoppingCartFactory.GetShoppingCart();

        public CartItemList()
        {
            ProductInfo product = Products.GetProduct(this.ProductId);
            if (product!=null)
            {
                switch (this.Type)
                {
                    case  CartActionType.Add:
                        AddToCart(product, this.Quantity);
                        break;
                    case CartActionType.Remove:
                        cart.Remove(ProductId);
                        break;
                    case CartActionType.ChangeQuantity:
                        if (this.cart.Contains(product.Id))
                        {
                            this.cart[this.ProductId].Quantity = Quantity;
                        }
                        else
                        {
                            AddToCart(product, this.Quantity);
                        }

                        break;
                }

                if (cart.Contains(this.ProductId))
                {
                    if (cart[this.ProductId].Quantity <= 0)
                    {
                        cart.Remove(this.ProductId);
                    }

                    UpdatePrice(this.ProductId, product.Price);
                }

                if (this.Type != CartActionType.None)
                {
                    var redirectUrl = SiteContext.Current.ReturnUrl;
                    if (string.IsNullOrEmpty(redirectUrl))
                    {
                        redirectUrl = SiteUrls.Urls.Cart();
                    }

                    SiteContext.Current.Context.Response.Redirect(redirectUrl);
                }
            }

            this.AspNetPagerControl.Visible = false;
        }
        private void AddToCart(ProductInfo product,int quantity)
        {
            ShoppingCartItem item = new ShoppingCartItem(product.Id, product.Title, ImageHelper.GetThumbnailUrl(product.ImageUrl, true), product.Price,product.Weight, quantity);
            item.Others.Add("productCode",product.ProductCode);
            item.Others.Add("color", Parameters.Length > 1 ? Parameters[1] : string.Empty);
            cart.Add(item);
        }
        private void UpdatePrice(int productId, decimal originalPrice)
        {
            if (!cart.Contains(productId))
                return;

            IShoppingCartItem item = cart[productId];
            item.UnitPrice = ProductWholesaleRule.PriceCalculation(item.Quantity, originalPrice);

            if (SiteContext.Current.Context.User.Identity.IsAuthenticated)
            {
                if (Users.UserIsVip(SiteContext.Current.User.Id))
                {
                    item.UnitPrice = item.UnitPrice * this.Settings.VipDiscount / 100;
                }
            }
        }

        protected override IList<IShoppingCartItem> GridViewDataSourceHandler(int pageIndex, int pageSize, out int totalRecord)
        {
            totalRecord = this.cart.Items.Count;
            return this.cart.Items.ToList();
        }

        public decimal TotalPrices
        {
            get { return this.cart.TotalPrice; }
        }

        public int ProductId
        {
            get { return SiteContext.Current.Id; }
        }

        public CartActionType Type
        {
            get { return (CartActionType)SiteContext.Current.Type; }
        }

        private int quantity = -1;
        public int Quantity
        {
            get 
            {
                if (quantity == -1)
                    quantity = Parameters[0].As<int>();

                return quantity;
            }
        }

        private string[] parameters;
        public string[] Parameters
        {
            get
            {
                if (parameters == null)
                {
                    parameters = SiteContext.Current.Parameters.Split('|');
                }

                return parameters;
            }
        }

        protected override string DefaultSkinFilename
        {
            get { return "CartItemList.ascx"; }
        }
    }
}
